﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace web.Pages.xml
{
    public partial class surveyadd_xml : System.Web.UI.Page
    {
        protected List<DiseaseSymptomInfo> PrimarySymptomList;

        protected string _sErrMsg;
        protected PatientRecordInfo PrData;
        protected UserInfo UserData = new UserInfo();// 当前用户的信息
         

        
        protected  void HandlePost()
        {
            bool succ = true;
            //检查是否登陆 
            UserData.Id = CookiesTools.GetUserIdCookie();
            if (UserData.Id > 0) //表示永久登陆的用户
            {
                UserData.UserName = CookiesTools.GetUserNameCookie();   //直接从cookie中获取用户名
                if (UserData.UserName.Length <= 0)
                {
                    UserData.Id = -1;    //如果不存在用户名，则为无效的cookie
                    _sErrMsg = "用户不存在";
                }
            }
            if (UserData.Id <= 0)
            {
                _sErrMsg = "还未登录";
                succ = false;
            }
            PrData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);

            //是否有患病信息
            if (PrData.Id <= 0)
            {
                _sErrMsg = "请先填写患病基本信息";
                succ = false;
            }
            
            //检查数据是否ok

            List<SymptomHistoryInfo> lst = ValidateForm();
            succ = lst != null;
           
            //加入
            if ( succ )
            {
                PatientSymptomBLL.Instance.AddSymptomSurvey(lst);
            }
            
            if (succ)
            {
                _sErrMsg = "添加成功";
            }
         
            //生产xml文档
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");
            Response.ContentType = "text/xml";
            using (XmlWriter wr = XmlWriter.Create(Response.OutputStream, settings))
            {
                // Write XML data.
                wr.WriteStartElement("result");
                wr.WriteAttributeString("code", succ ? "0":"-1");
                wr.WriteAttributeString("msg", _sErrMsg);
                wr.WriteEndElement();
                wr.Flush();
            }
        }

        protected List<SymptomHistoryInfo> ValidateForm()
        {
            // 对时间进行判断
            string value = RequestTools.GetFormString("date");
            if (value == "")
            {
                _sErrMsg = "请填写日期！";
                return null;
            }

            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                _sErrMsg = "错误的日期：" + value;
                return null;
            }

            if (DateTime.Compare(date, DateTime.Today) > 0)
            {
                _sErrMsg = "记录日期 " + date.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！";
                return null;
            }
            else if (DateTime.Compare(date, PrData.StartDate) < 0)
            {
                _sErrMsg = "记录日期 " + date.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！";
                return null;
            }

            //需要判断是否有相同时间的历史记录
            if (PatientBLL.Instance.CheckPatientSurvey(PrData.Id, Constants.SymptomSurvey, date))
            {
                //提示是否需要覆盖历史记录
                _sErrMsg = "是否需要覆盖" + PrData.RecordDate.ToString("yyyy-MM-dd") + "已有的批量历史记录？";
                return null;
            }

            List<SymptomHistoryInfo> lst = new List<SymptomHistoryInfo>();
            foreach (DiseaseSymptomInfo item in PrimarySymptomList)
            {
                SymptomHistoryInfo info = new SymptomHistoryInfo();
                info.PRId = PrData.Id;
                info.SymptomId = item.SymptomId;
                info.DegreeValue = RequestTools.GetFormInt("sym" + item.SymptomId, -1);
                info.RecordDate = date;
                info.UpdateDate = DateTime.Today;
                info.Type = Constants.PRIMARY_SYMPTOM;
                lst.Add(info);
            }

           
            return lst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            HandlePost();
        }
    }
}
